package com.cold.demo.openoffice;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.net.ConnectException;

/**
 * @Author x c
 * @Email 378661553@qq.com
 * @Date Created in 2019/12/10 16:16
 * 要使用该类必须先安装软件OpenOffice，并启动起来将端口设置为 SocketOpenOfficeConnection 连接的端口
 */
public class Word2Pdf {


    OpenOfficeConnection connection;
    String openOfficeIp = "192.168.13.131";
    int openOfficePort = 8200;

    public Word2Pdf() {
        init();
        Runtime.getRuntime().addShutdownHook(new Thread(this::destroy));
    }

    private void init() {
        connection = new SocketOpenOfficeConnection(openOfficeIp, openOfficePort);
        try {
            System.out.println("开始进行连接");
            connection.connect();
            System.out.println("连接成功。。");
        } catch (ConnectException e) {
            System.out.println("连接失败。。");
            e.printStackTrace();
            String msg = "请先开启 OpenOffice 进程" +
                    "\nWindows下路径：C:/Program Files (x86)/OpenOffice 4/program" +
                    "\nLinux下路径：/opt/openoffice4/program" +
                    "\nWindows下的命令：soffice \"-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager\" -nologo -headless -nofirststartwizard" +
                    "\nLinux下的命令：soffice \"-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager\" -nologo -headless -nofirststartwizard &";
            System.out.println(msg);
            throw new RuntimeException(e);
        }
    }

    private void destroy() {
        System.out.println("开始关闭连接...");
        connection.disconnect();
        System.out.println("关闭连接结束...");
    }

    public static void main(String[] args) throws FileNotFoundException {
        try {
            String pathRoot = "D:\\Users\\zyc\\Desktop\\";
            Word2Pdf word2Pdf = new Word2Pdf();
            File input = new File(pathRoot+"123.html");
            File output = new File(pathRoot+"123.pdf");


            System.out.println(input.exists());
            System.out.println(output.exists());
            word2Pdf.convert(
                    new FileInputStream(input),word2Pdf.guessDocumentFormat(input),
                    new FileOutputStream(output),word2Pdf.guessDocumentFormat(output));
        }finally {
            System.out.println("转换完毕");
            System.exit(0);
        }

    }


    public void convert(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat){
        DocumentConverter converter = getDocumentConverter();
        converter.convert(inputStream, inputFormat, outputStream, outputFormat);
    }


    public void convert(File inputFile, DocumentFormat inputFormat, File outputFile, DocumentFormat outputFormat){
        DocumentConverter converter = getDocumentConverter();
        converter.convert(inputFile, inputFormat, outputFile, outputFormat);
    }


    public void convert(File inputDocument, File outputDocument, DocumentFormat outputFormat){
        DocumentConverter converter = getDocumentConverter();
        converter.convert(inputDocument, outputDocument, outputFormat);
    }


    public void convert(File inputDocument, File outputDocument){
        DocumentConverter converter = getDocumentConverter();
        converter.convert(inputDocument, outputDocument);
    }


    private DocumentConverter getDocumentConverter() {
//        return new OpenOfficeDocumentConverter(connection);
        return new StreamOpenOfficeDocumentConverter(connection);
    }


    private DocumentFormat guessDocumentFormat(File file) {
        String extension = FilenameUtils.getExtension(file.getName());
        DocumentFormat format = new DefaultDocumentFormatRegistry().getFormatByFileExtension(extension);
        if (format == null) {
            throw new IllegalArgumentException("unknown document format for file: " + file);
        }
        return format;
    }
}
